<!-- 新增编辑版本 -->
<template>
  <div>
    <el-dialog
      :title="isAdd == 1 ? '新增App历史版本' : '修改App历史版本'"
      :visible.sync="addEditVisible"
    >
      <el-form
        class="formView"
        ref="form"
        :model="formdata"
        :rules="rules"
        label-position="right"
        label-width="120px"
      >
        <el-form-item label="是否强制更新:" prop="type">
          <el-select v-model="formdata.type" placeholder="请选择" clearable>
            <el-option label="是" :value="1"></el-option>
            <el-option label="否" :value="2"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="版本号:" prop="versionCode">
          <el-input
            v-model.number="formdata.versionCode"
            :disabled="isAdd == 2"
            placeholder="请输入"
          ></el-input>
        </el-form-item>

        <el-form-item label="版本名:" prop="version">
          <el-input
            v-model="formdata.version"
            :disabled="isAdd == 2"
            placeholder="请输入"
          ></el-input>
        </el-form-item>

        <el-form-item label="发布渠道" prop="phoneType">
          <el-select
            v-model="formdata.phoneType"
            placeholder="请选择"
            @change="phoneTypeChange"
            clearable
            :disabled="isAdd == 2"
          >
            <el-option label="Android" :value="1"></el-option>
            <el-option label="IOS" :value="2"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="" prop="channel">
          <el-select
            v-model="formdata.channel"
            placeholder="请选择"
            clearable
            :disabled="isAdd == 2"
          >
            <el-option
              v-for="item in formdata.phoneType === 1
                ? androidOptions
                : formdata.phoneType === 2
                ? iosOptions
                : []"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="更新文案:" prop="contents">
          <el-input
            v-model="formdata.contents"
            placeholder="请输入"
            type="textarea"
            :rows="3"
            :maxlength="100"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="addEditVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addVersionHistory, updateHistoryVersion } from '@/api/rightsManage'
export default {
  components: {},
  props: {
    isAdd: {
      type: Number,
      default() {
        return 1
      }
    },
    detailData: {
      type: Object,
      default() {
        return {}
      }
    },
    androidOptions: {
      type: Array,
      default() {
        return []
      }
    },
    iosOptions: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      addEditVisible: false,
      formdata: {
        type: '',
        versionCode: '',
        version: '',
        phoneType: '',
        channel: '',
        contents: '',
        versionId: ''
      },
      rules: {
        type: [
          { required: true, message: '请选择是否强制更新', trigger: 'blur' }
        ],
        versionCode: [
          {
            type: 'number',
            required: true,
            message: '请输入版本号(仅支持数字)',
            trigger: 'blur'
          }
        ],
        version: [{ required: true, message: '请输入版本名', trigger: 'blur' }],
        contents: [
          { required: true, message: '请输入更新文案', trigger: 'blur' }
        ],
        phoneType: [
          { required: true, message: '请选择手机类型', trigger: 'blur' }
        ],
        channel: [{ required: true, message: '请选择渠道', trigger: 'blur' }]
      }
    }
  },
  created() {},
  mounted() {},
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.onSubmit()
        } else {
          return false
        }
      })
    },
    // 确认
    onSubmit() {
      if (this.isAdd == 1) {
        addVersionHistory(this.formdata).then(res => {
          this.$message({
            message: this.isAdd == 1 ? '新增成功' : '修改成功',
            type: 'success'
          })
          this.$parent.getDataList()
          this.resetForm('form')
        })
      } else if (this.isAdd == 2) {
        updateHistoryVersion(this.formdata).then(res => {
          this.$message({
            message: this.isAdd == 1 ? '新增成功' : '修改成功',
            type: 'success'
          })
          this.$parent.getDataList()
          this.resetForm()
        })
      }
    },
    resetForm() {
      this.formdata = {
        type: '',
        versionCode: '',
        version: '',
        phoneType: '',
        channel: '',
        contents: '',
        versionId: ''
      }
      this.addEditVisible = false
    },
    phoneTypeChange() {
      this.formdata.channel = ''
    }
  },
  watch: {
    addEditVisible: {
      deep: true,
      handler(now, old) {
        if (now) {
          if (this.isAdd == 2) {
            this.formdata = Object.assign({}, this.formdata, this.detailData)
          }
        } else {
          this.resetForm('form')
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep {
  .el-dialog {
    width: 500px;
    border-radius: 10px;
  }
  .el-dialog__title {
    font-size: 16px;
  }
  .el-input,
  .el-textarea {
    width: 250px;
  }
  .el-dialog__body {
    padding-top: 0;
    padding-bottom: 0px;
  }
  .el-form {
    padding: 10px 0;
  }
  .el-input__inner {
    height: 35px;
    line-height: 35px;
  }
  .el-form-item {
    margin-bottom: 15px;
  }
  .el-dialog__footer {
    padding-top: 10px;
    padding-bottom: 10px;
    border-top: 1px solid #eee;
  }
}
.formView {
  border-top: 1px solid #eee;
  .sortInput {
    width: 100px;
  }
}
.inline-input {
  ::v-deep {
    .el-input {
      width: 125px;
    }
  }
}
</style>
